import {
  createRouter,
  createWebHistory
} from 'vue-router'
import {
  useTokenStore
} from '@/stores/user'
import {
  ElMessageBox
} from 'element-plus'

const router = createRouter({
  history: createWebHistory(
    import.meta.env.BASE_URL),
  routes: [{
      path: '/',
      name: 'home',
      component: () => import('../views/AiChatView.vue'),
      meta: {
        requireAuth: true,
        requireOpenid: false
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
      meta: {
        requireAuth: false,
        requireOpenid: false
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import("@/views/RegisterView.vue"),
      meta: {
        requireAuth: false,
        requireOpenid: false
      }
    },
    {
      path: '/wechat_login',
      name: 'wechat_login',
      component: () => import("@/views/WechatLoginView.vue"),
      meta: {
        requireAuth: false,
        requireOpenid: false
      }
    },
    {
      path: '/bind_account',
      name: 'bind_account',
      component: () => import("@/views/WechatBindAccountView.vue"),
      meta: {
        requireAuth: false,
        requireOpenid: true
      }
    },
    {
      path: '/ai_chat',
      name: 'ai_chat',
      component: () => import('../views/AiChatView.vue'),
      meta: {
        requireAuth: true,
        requireOpenid: false
      }
    },
    {
      path: '/userCenter',
      name: 'userCenter',
      component: () => import("@/views/UserCenter.vue"),
      meta: {
        requireAuth: true,
        requireOpenid: false
      },
      children: [{
          path: '/EditUser',
          name: 'EditUser',
          component: () => import('@/components/EditUser.vue'),
          meta: {
            requireAuth: true,
            requireOpenid: false
          },
        },
        {
          path: '/BuyToken',
          name: 'BuyToken',
          component: () => import('@/components/BuyToken.vue'),
          meta: {
            requireAuth: true,
            requireOpenid: false
          },
        },
        {
          path: '/PaymentRecords',
          name: 'PaymentRecords',
          component: () => import('@/components/PaymentRecords.vue'),
          meta: {
            requireAuth: true,
            requireOpenid: false
          },
        }
      ]
    }
  ]
})

router.beforeEach(async (to, from) => {
  const store = useTokenStore();
  if (!store.Authorization && to.meta.requireAuth) {

    console.log("路由守卫拦截");

    try {
      await ElMessageBox.alert('您还没有登录', '未登录', {
        confirmButtonText: '登陆',
      });
      localStorage.clear();
      router.push('/login');

    } catch (error) {
      console.log("路由error" + error);
    }

    return false;
  } else if (!store.openid && to.meta.requireOpenid) {
    console.log("路由守卫拦截");

    try {
      await ElMessageBox.alert('您还没有进行微信授权', '未授权', {
        confirmButtonText: '微信授权',
      });
      localStorage.clear();
      router.push('/wechat_login');

    } catch (error) {
      console.log("路由error" + error);
    }

    return false;
  } else {
    return true;
  }


})

export default router